Skip to content

fix: BigQuery finops SQL — correct INFORMATION_SCHEMA columns + multi-region support#739

Merged
anandgupta42 merged 1 commit intomainfrom
fix/bigquery-query-history-columns
Apr 23, 2026
Merged

fix: BigQuery finops SQL — correct INFORMATION_SCHEMA columns + multi-region support#739
anandgupta42 merged 1 commit intomainfrom
fix/bigquery-query-history-columns

Conversation

@anandgupta42
Copy link
Copy Markdown
Contributor

@anandgupta42 anandgupta42 commented Apr 23, 2026

Summary

finops_query_history was failing 100% on BigQuery with Unrecognized name: error_message at [11:5]. Telemetry caught it as a single session looping 76 times over 2.5 hours. Root cause: the BQ SQL template in packages/opencode/src/altimate/native/finops/query-history.ts selected three columns that do not exist in INFORMATION_SCHEMA.JOBS, plus a broader issue: all five finops modules hardcoded region-US, making the tools unusable for non-US BigQuery projects.

Column fixes in BIGQUERY_HISTORY_SQL

  • error_message does not exist as a top-level column → now error_result.message AS error_message.
  • error_result.reason AS error_code in place of the previous hardcoded NULL (more useful; matches the struct that actually exists).
  • total_rows is a PARTITIONS column, not JOBSCAST(NULL AS INT64) AS rows_produced.
  • BQ's state returns 'DONE', not 'SUCCESS'. getQueryHistory() counts execution_status != 'SUCCESS' as a failure, so bare state AS execution_status was flagging every completed BQ job as failed. Now derives CASE WHEN error_result IS NULL THEN 'SUCCESS' ELSE 'FAILED' END.

Multi-region support across all 5 finops modules

Replaced hardcoded region-US with a {region} placeholder in every BQ INFORMATION_SCHEMA query — threaded from the connection's configured location at runtime. New shared helper packages/opencode/src/altimate/native/finops/bq-utils.ts:

  • sanitizeBqRegion — allowlist [a-z0-9-], trims leading/trailing hyphens, caps at 64 chars, falls back to "us".
  • interpolateBqRegion — uses replaceAll so future templates that JOIN multiple INFORMATION_SCHEMA views remain safe.
  • bqRegionFor(warehouse) — reads Registry.getConfig(warehouse).location.

Covers query-history, credit-analyzer (3 templates), warehouse-advisor (2 templates), role-access, and unused-resources.

Snowflake / Databricks: zero blast radius

The new bqRegion? parameter is optional on every build helper and only consumed inside whType === "bigquery" branches. Regression tests explicitly assert Snowflake and Databricks SQL still contains QUERY_HISTORY / system.query.history and never contains region-.

Test plan

  • Unit: packages/opencode/test/altimate/schema-finops-dbt.test.ts — 75 pass (was 66). New coverage: column-level regression guards (no bare error_message / total_rows / state as execution_status); table-driven test that no finops BQ template contains region-US; region-threading for all 5 build helpers; sanitizer (injection, hyphen-trim, length cap, non-string); Snowflake + Databricks regression guards.
  • E2E: new packages/opencode/test/altimate/finops-bigquery-e2e.test.ts, skipIf-gated on ALTIMATE_CODE_CONN_BIGQUERY_TEST env var, mirrors the Snowflake pattern. Verifies shape, no SQL parse errors, derived execution_status, error_count math, bad-region graceful-failure. 10/10 pass against real BigQuery (US region) using a service-account key kept outside the repo.
  • Full altimate test suite: 2,917 / 2,917 pass (was 2,908; +9 new assertions, 0 failures, 438 skip).
  • bun turbo typecheck — clean across monorepo.
  • Upstream marker check (bun run script/upstream/analyze.ts --markers --base main --strict) — clean (these are Altimate-specific native files, not upstream-shared).
  • /consensus:code-review — GPT-5.4, Gemini 3.1 Pro, and DeepSeek V3.2 all APPROVE on convergence round 1 after incorporating their MAJOR findings:
    • GPT-5.4: missing region-threading unit tests for credit-analyzer + warehouse-advisor → added.
    • Gemini 3.1 Pro: caught that role-access.ts and unused-resources.ts still had hardcoded region-US → fixed.
    • DeepSeek V3.2: sanitizer should trim hyphens + cap length → applied.

Checklist

  • Tests added/updated
  • Documentation updated (if needed) — N/A; templates are the doc.
  • CHANGELOG updated (if user-facing) — N/A; fix-only, no user-facing API change.

Closes #738

Summary by CodeRabbit

  • New Features

    • BigQuery FinOps features now support multiple regions dynamically instead of being limited to the US region. Query history, credit analysis, warehouse advisor, unused resource detection, and role access analysis now adapt to your warehouse's configured region.
  • Tests

    • Added comprehensive integration and unit tests for multi-region BigQuery FinOps support.

Summary by cubic

Fixes BigQuery FinOps failures by using the correct INFORMATION_SCHEMA.JOBS fields and deriving execution status, eliminating “Unrecognized name” errors. Adds BigQuery multi‑region support by threading the connection location into all FinOps queries.

  • Bug Fixes

    • Use error_result.message AS error_message and error_result.reason AS error_code.
    • Replace total_rows with CAST(NULL AS INT64) AS rows_produced (not a JOBS column).
    • Derive execution_status via CASE WHEN error_result IS NULL THEN 'SUCCESS' ELSE 'FAILED' END (BQ state is 'DONE').
  • New Features

    • Replace hardcoded region-US with {region} in all BigQuery templates; interpolate from Registry.getConfig(...).location.
    • New helper bq-utils with sanitizeBqRegion, interpolateBqRegion, and bqRegionFor.
    • Applied across query-history, credit-analyzer, warehouse-advisor, role-access, and unused-resources.
    • Snowflake/Databricks paths unchanged; added unit + BigQuery E2E tests to prevent regressions.

Written for commit 03a9b37. Summary will update on new commits.

…-region support

`finops_query_history` was failing 100% on BigQuery with
`Unrecognized name: error_message at [11:5]`. Telemetry traced it to one
session looping 76 times over 2.5 hours. Three separate bugs in the BQ
query-history template plus a broader region-locking issue across the
whole finops module.

Fixes in `packages/opencode/src/altimate/native/finops/query-history.ts`
BIGQUERY_HISTORY_SQL:

- `error_message` does not exist as a top-level column in
  INFORMATION_SCHEMA.JOBS. Replace with `error_result.message AS error_message`.
- `error_result.reason AS error_code` in place of the previous hardcoded NULL
  (more useful and matches the struct that actually exists).
- `total_rows` is a PARTITIONS column, not JOBS. Replace with
  `CAST(NULL AS INT64) AS rows_produced` so the downstream summary loop
  doesn't error.
- BQ's `state` returns `'DONE'`, not `'SUCCESS'`. `getQueryHistory()` counts
  any `execution_status != 'SUCCESS'` as a failure, so bare
  `state AS execution_status` was flagging every completed BQ job as failed.
  Derive `CASE WHEN error_result IS NULL THEN 'SUCCESS' ELSE 'FAILED' END`
  to match the other warehouse templates.

Multi-region support across all 5 finops modules:

- All BQ `INFORMATION_SCHEMA` queries were hardcoded to
  `` `region-US.INFORMATION_SCHEMA.*` ``, making the tools unusable for
  non-US BigQuery projects. Replace with `{region}` placeholder and
  interpolate from the connection's configured `location` at runtime.
- New shared helper `finops/bq-utils.ts` exposes `sanitizeBqRegion`
  (allowlist `[a-z0-9-]`, trim hyphens, cap at 64 chars, fall back to `us`),
  `interpolateBqRegion` (uses `replaceAll` so future JOIN-across-views
  templates stay safe), and `bqRegionFor(warehouse)`.
- Covers query-history, credit-analyzer (3 templates), warehouse-advisor
  (2 templates), role-access, and unused-resources.

Snowflake and Databricks paths are untouched. The new `bqRegion?` parameter
is optional on every build helper and only consumed inside
`whType === "bigquery"` branches; regression tests assert Snowflake and
Databricks SQL still contains `QUERY_HISTORY` / `system.query.history` and
never `region-`.

Tests:

- New `schema-finops-dbt.test.ts` assertions: column-level regression guards
  (no bare `error_message`/`total_rows`/`state as execution_status`), a
  table-driven test that no finops BQ template contains `region-US`,
  region-threading tests for all 5 build helpers, sanitizer coverage
  (injection, hyphen-trim, length cap, non-string), Snowflake/Databricks
  regression guards. 75 pass (up from 66).
- New `finops-bigquery-e2e.test.ts`: skipIf-gated against
  `ALTIMATE_CODE_CONN_BIGQUERY_TEST`, mirrors the Snowflake pattern.
  Verifies shape, no SQL parse errors, derived `execution_status`,
  error_count math, and a bad-region graceful-failure path. 10/10 pass
  against real BigQuery (US region).
- Full altimate suite: 2917/2917 pass (was 2908). Typecheck clean.

Reviewed via /consensus:code-review by GPT-5.4, Gemini 3.1 Pro, and
DeepSeek V3.2 — all three APPROVE on round 1 after incorporating their
MAJOR findings (missing tests, missed files, sanitizer hardening).

Closes #738

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

@claude claude Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review to trigger a review and subscribe this PR to future pushes, or @claude review once for a one-time review.

Tip: disable this comment in your organization's Code Review settings.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 23, 2026

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

This PR introduces region-aware BigQuery finops queries by adding utility functions for region sanitization and SQL template interpolation, updating all finops SQL templates to use dynamic {region} placeholders instead of hard-coded region-US, fixing broken column references in BigQuery query-history SQL, and adding comprehensive E2E and unit tests for BigQuery warehouse support.

Changes

Cohort / File(s) Summary
Region Utilities
packages/opencode/src/altimate/native/finops/bq-utils.ts
New module exporting sanitizeBqRegion() to normalize region strings, interpolateBqRegion() to replace {region} placeholders in SQL templates, and bqRegionFor() to retrieve configured warehouse location from the connections registry.
FinOps Modules with Region Support
packages/opencode/src/altimate/native/finops/credit-analyzer.ts, query-history.ts, role-access.ts, unused-resources.ts, warehouse-advisor.ts
Updated SQL templates to use {region} placeholders instead of hard-coded region-US. SQL builder functions now accept optional bqRegion parameter and interpolate it at runtime. For query-history.ts: corrected column references from non-existent top-level error_message and total_rows to error_result.message, error_result.reason, and NULL respectively; derived execution_status from error_result presence instead of raw state.
BigQuery E2E Tests
packages/opencode/test/altimate/finops-bigquery-e2e.test.ts
New integration test suite covering query_history, analyzeCredits/getExpensiveQueries, and adviseWarehouse finops capabilities with response shape validation, required fields/columns checks, and region sanity verification for invalid locations.
FinOps Schema & Unit Tests
packages/opencode/test/altimate/schema-finops-dbt.test.ts
Expanded test coverage for BigQuery query-history SQL correctness, error_result STRUCT field validation, region substitution behavior, and direct unit tests for sanitizeBqRegion() and interpolateBqRegion() utilities. Added assertions ensuring BigQuery SQL templates use {region} and never hard-code region-US, while Snowflake/Databricks paths remain unaffected.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Suggested labels

contributor


🐰 A whisker-twitch of joy! Region-bound queries now roam free,
From hard-coded US to dynamic jubilee,
BigQuery's INFORMATION\_SCHEMA revealed its hidden art,
With error\_result STRUCTs and tests from the start,
Now every warehouse finds its place, near or far away! 🌍✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 55.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main changes: fixing BigQuery INFORMATION_SCHEMA column issues and adding multi-region support across finops modules.
Description check ✅ Passed The description includes PINEAPPLE marker, covers Summary, Test Plan, and Checklist sections with comprehensive details on column fixes, multi-region support implementation, and test results.
Linked Issues check ✅ Passed All objectives from issue #738 are met: column fixes applied (error_message, error_code, rows_produced, execution_status), multi-region support added, unit and E2E tests added, Snowflake/Databricks unchanged.
Out of Scope Changes check ✅ Passed All changes are directly tied to fixing #738 and implementing multi-region support. New bq-utils.ts, template updates, and test coverage are all in scope; retry-loop improvements are explicitly marked as out of scope.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/bigquery-query-history-columns

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown

👋 This PR was automatically closed by our quality checks.

Common reasons:

  • New GitHub account with limited contribution history
  • PR description doesn't meet our guidelines
  • Contribution appears to be AI-generated without meaningful review

If you believe this was a mistake, please open an issue explaining your intended contribution and a maintainer will help you.

@github-actions github-actions Bot closed this Apr 23, 2026
@anandgupta42 anandgupta42 reopened this Apr 23, 2026
anandgupta42 added a commit that referenced this pull request Apr 23, 2026
…741)

The `anti-slop` workflow was closing legitimate team-member PRs within
two minutes of opening. Root cause is a self-inflicted honeypot: the
repo's `pull_request_template.md` embeds an HTML comment instructing any
LLM/AI model to prepend the word "PINEAPPLE" to the PR description, and
`anti-slop.yml` lists that same word in `blocked-terms`. Any AI-assisted
PR from a team member follows the template instruction verbatim and then
trips the blocked-terms check — a guaranteed close. PR #739 (an
8-file, 496-line BQ finops fix with a passing consensus code review and
all other CI green) was auto-closed this way.

There is no `TEAM_MEMBERS` carve-out in `anti-slop.yml` the way
`pr-standards.yml` has one, so even listed members are subject to the
auto-close. The action's `authorAssociation` check only recognizes
GitHub's built-in OWNER/MEMBER/COLLABORATOR, not the repo's local
`.github/TEAM_MEMBERS` file.

Change: flip `close-pr: true` -> `close-pr: false`. All other anti-slop
behavior is preserved — failure labels still land (`needs-review:blocked`),
the failure message still comments on the PR, and the action still runs
on every open/edit/synchronize. Maintainers can close manually when the
signal is real. This keeps the anti-spam signal without the false-positive
close.

Two follow-ups are noted in the in-file comment and issue #740 but left
out of scope here:

1. Whether to keep `PINEAPPLE` in `blocked-terms` at all given that the
   template actively induces its inclusion.
2. Whether to add a `TEAM_MEMBERS` carve-out to anti-slop mirroring
   `pr-standards.yml`'s pattern.

Closes #740

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@anandgupta42 anandgupta42 merged commit ea5cabe into main Apr 23, 2026
31 checks passed
anandgupta42 added a commit that referenced this pull request Apr 24, 2026
Patch release. User-facing: BigQuery finops tools were 100%-broken and are
now fixed, and work in any BigQuery region (previously hardcoded to US).

Changes since v0.6.0:

- fix: BigQuery finops SQL — correct INFORMATION_SCHEMA columns + multi-region
  support (#739, closes #738). `error_message`/`error_code`/`total_rows` bugs
  plus `state='SUCCESS'` vs BQ's actual `'DONE'` (every completed job was
  being reported as FAILED). All 5 finops modules now read the BQ
  connection's `location` via a sanitised `{region}` placeholder.
- fix: make anti-slop workflow advisory instead of auto-closing legitimate
  PRs (#741, closes #740).
- fix: marker-guard hotfix for isValidDatabricksHost env-fallback path.
- docs: 12 end-to-end showcase examples + `location` documented for BQ (#742).

Review: 5-persona review (CTO/PM/DE/Tech Lead/Chaos Gremlin) ran on main —
no P0s, no HOLDs. 1 P1 (silent `us` fallback compliance concern — addressed
via docs + changelog warnings; behaviour-change follow-ups tracked in #754).
2 HIGH docs-accuracy items fixed pre-tag: `location` row in drivers.md and
finops-tools.md.

Tests:
- 47 new adversarial tests in test/skill/release-v0.6.1-adversarial.test.ts
  (sanitizeBqRegion injection vectors, interpolateBqRegion idempotency,
  bqRegionFor registry edge cases, BIGQUERY_HISTORY_SQL column-name
  regression guards for all four #739 bugs, cross-module {region} guard,
  buildHistoryQuery full-pipeline incl. Snowflake/Databricks no-regression).
- 409/409 tests pass across finops + skill suites. Full altimate suite
  previously green (2917/2917) on #739 merge.
- Typecheck: 5/5 pass. Marker guard: clean. Pre-release sanity: all 4 green.

Deferred: BQ finops UX + robustness follow-ups filed as #754 (surface
queried region in responses, distinguish no-location vs invalid-location,
warn at warehouse_add time, friendlier perms error, options-object
refactor, e2e silent-skip warning).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

finops_query_history fails on BigQuery — invalid column names in SQL template

1 participant